package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.vo.category.CategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;
	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;
	@Autowired
	private BaseAttributeMapper baseAttributeMapper;
	@Autowired
	private AlbumInfoMapper albumInfoMapper;


	@Override
	public List<CategoryVo> getBaseCategoryList() {
	return baseCategoryViewMapper.getBaseCategoryList();

//		ArrayList<CategoryVo> categoryVoList = new ArrayList<>();
//		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
//		System.out.println("baseCategoryViews = " + baseCategoryViews);
//		Map<Long, List<BaseCategoryView>> category1IdAndChildrenMap = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
//		for (Map.Entry<Long, List<BaseCategoryView>> category1IdAndChildrenEntry : category1IdAndChildrenMap.entrySet()) {
//			CategoryVo category1Vo = new CategoryVo();
//			category1Vo.setCategoryId(category1IdAndChildrenEntry.getKey());
//			List<BaseCategoryView> category1ChildrenList = category1IdAndChildrenEntry.getValue();
//			category1Vo.setCategoryName(category1ChildrenList.get(0).getCategory1Name());
//
//			Map<Long, List<BaseCategoryView>> category2IdAndChildrenMap = category1ChildrenList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
//			ArrayList<CategoryVo> category2Vos = new ArrayList<>();
//			for (Map.Entry<Long, List<BaseCategoryView>> category2IdAndChildrenEntry : category2IdAndChildrenMap.entrySet()) {
//				CategoryVo category2Vo = new CategoryVo();
//				category2Vo.setCategoryId(category2IdAndChildrenEntry.getKey());
//				List<BaseCategoryView> category2ChildrenList = category2IdAndChildrenEntry.getValue();
//				category2Vo.setCategoryName(category2ChildrenList.get(0).getCategory2Name());
//				ArrayList<CategoryVo> category3Vos = new ArrayList<>();
//				for (BaseCategoryView baseCategoryView : category2ChildrenList) {
//					CategoryVo category3Vo = new CategoryVo();
//					category3Vo.setCategoryId(baseCategoryView.getCategory2Id());
//					category3Vo.setCategoryName(baseCategoryView.getCategory3Name());
//					category3Vos.add(category3Vo);
//				}
//				category2Vo.setCategoryVoList(category3Vos);
//				category2Vos.add(category2Vo);
//			}
//			category1Vo.setCategoryVoList(category2Vos);
//			categoryVoList.add(category1Vo);
//		}
//		return categoryVoList;
    }

    @Override
    public List<BaseAttribute> findAttributeAndValue(Long c1Id) {
        return baseAttributeMapper.findAttributeAndValue(c1Id);
    }

    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id));
		if (CollectionUtils.isEmpty(baseCategory2List)){
			throw new GuiguException(201,"该一级分类下没有二级分类");
		}
		List<Long> category2IdList = baseCategory2List.stream().map(BaseCategory2::getId).collect(Collectors.toList());
		List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>()
				.in(BaseCategory3::getCategory2Id, category2IdList)
				.eq(BaseCategory3::getIsTop, 1)
				.last("limit 7"));
		if (CollectionUtils.isEmpty(baseCategory3List)){
			return new ArrayList<>();
		}
		return baseCategory3List;
    }

    @Override
    public BaseCategoryView getAlbumInfoCategory(Long albumId) {
		AlbumInfo albumInfo = albumInfoMapper.selectById(albumId);
		if (albumInfo == null){
			throw new GuiguException(201,"专辑信息不存在");
		}
		Long category3Id = albumInfo.getCategory3Id();
		BaseCategory3 baseCategory3 = baseCategory3Mapper.selectById(category3Id);
		Long category2Id = baseCategory3.getCategory2Id();

		BaseCategory2 baseCategory2 = baseCategory2Mapper.selectById(category2Id);
		Long category1Id = baseCategory2.getCategory1Id();
		BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(category1Id);

		BaseCategoryView baseCategoryView = new BaseCategoryView();
		baseCategoryView.setCategory1Id(category1Id);
		baseCategoryView.setCategory2Id(category2Id);
		baseCategoryView.setCategory3Id(category3Id);
		baseCategoryView.setCategory1Name(baseCategory1.getName());
		baseCategoryView.setCategory2Name(baseCategory2.getName());
		baseCategoryView.setCategory3Name(baseCategory3.getName());
		return baseCategoryView;

	}

    @Override
    public CategoryVo getBaseCategoryListByCategory1Id(Long category1Id) {
		CategoryVo categoryVo1 = new CategoryVo();
		BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(category1Id);

		if (baseCategory1 != null){
			categoryVo1.setCategoryId(category1Id);
			categoryVo1.setCategoryName(baseCategory1.getName());
			List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList
					(new LambdaQueryWrapper<BaseCategory2>()
							.eq(BaseCategory2::getCategory1Id, category1Id));

			if (!CollectionUtils.isEmpty(baseCategory2List)){
				List<CategoryVo> categoryVo2List = baseCategory2List.stream().map(baseCategory2 -> {
					CategoryVo categoryVo2 = new CategoryVo();
					categoryVo2.setCategoryId(baseCategory2.getId());
					categoryVo2.setCategoryName(baseCategory2.getName());
					List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList
							(new LambdaQueryWrapper<BaseCategory3>()
									.eq(BaseCategory3::getCategory2Id, baseCategory2.getId()));

					if (!CollectionUtils.isEmpty(baseCategory3List)) {
						List<CategoryVo> categoryVo3List = baseCategory3List.stream().map(baseCategory3 -> {
							CategoryVo categoryVo3 = new CategoryVo();
							categoryVo3.setCategoryId(baseCategory3.getId());
							categoryVo3.setCategoryName(baseCategory3.getName());
							categoryVo3.setCategoryChild(new ArrayList<>());
							return categoryVo3;
						}).collect(Collectors.toList());
						categoryVo2.setCategoryChild(categoryVo3List);
					}
					return categoryVo2;
				}).collect(Collectors.toList());
				categoryVo1.setCategoryChild(categoryVo2List);
			}
		}
		return categoryVo1;
    }

    @Override
    public List<Long> getCategory1IdList() {
		List<BaseCategory1> category1List = baseCategory1Mapper.selectList(null);
        return category1List.stream().map(BaseCategory1::getId).collect(Collectors.toList());
    }
}
